




/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/15/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.yfprojectpilot.struts.actions;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.usertable.entity.Usertable;
import com.application.yfexamine.entity.YfExmaine;
import com.application.yfexamine.service.YfExmaineService;
import com.application.yfexamine.struts.form.YfExmaineForm;
import com.application.yfprojectdesign.entity.YfProjectDesign;
import com.application.yfprojectdesign.struts.form.YfProjectDesignForm;
import com.application.yfprojectpilot.commons.Constants;
import com.application.yfprojectpilot.entity.YfProjectPilot;
import com.application.yfprojectpilot.service.YfProjectPilotService;
import com.application.yfprojectpilot.struts.form.YfProjectPilotForm;
import com.application.yfprojectteams.entity.YfProjectTeams;
import com.application.yfprojectteams.service.YfProjectTeamsService;
import com.application.yfprojectteams.struts.form.YfProjectTeamsForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.DisplayTagPageBean;
import com.manager.commons.RandomKey;
import com.manager.commons.page.PageBean;
import com.manager.workflow.WorkFlowConstants;
import com.manager.workflow.core.WorkFlowCommon;
import com.util.ApplicationUtil;
 /**
 * 
 * Description:YfProjectPilotAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060    <br />
 * 
 * History: <br />
 * 
 * 02/15/2012 
 * 
 */
public class YfProjectPilotAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfProjectPilotService yfProjectPilotService;
	
	private AttachmentService attachmentService;
	
	private WorkFlowCommon workFlowCommon;
	
	private YfExmaineService yfExmaineService;
	
	private YfProjectTeamsService yfProjectTeamsService;


	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
        private YfProjectPilot yfProjectPilot;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfProjectPilotPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfProjectPilot" + "开始");
		YfProjectPilotForm yfProjectPilot = (YfProjectPilotForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfProjectPilotList = null;
		resultSize = yfProjectPilotService.getAllCountYfProjectPilot(yfProjectPilot);
		pageBean = PageBean.getPageBean("yfProjectPilotTab", resultSize,request);
		yfProjectPilotList = yfProjectPilotService.searchYfProjectPilot(yfProjectPilot,pageBean);
		request.setAttribute("yfProjectPilotList", yfProjectPilotList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());

		log.debug("searchYfProjectPilot" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfProjectPilot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfProjectPilots");
		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		yfProjectPilotForm.setId(RandomKey.generateID_Long_19());
		ApplicationUtil au = new ApplicationUtil();
		List<Usertable> userList =  au.getUserByName("研发经理",null);

	    request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
	    request.setAttribute("userList", userList);

		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfProjectPilot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfProjectPilots");
		YfProjectPilot yfProjectPilot = this.getYfProjectPilot(form);
		yfProjectPilot.setOperateid(CommonUtil.getCurrentUser(request).getId());
		yfProjectPilot.setCreateDate(new Date());
		yfProjectPilot.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_1);
		this.yfProjectPilotService.insertObject(yfProjectPilot);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfProjectPilot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfProjectPilots");
		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		this.yfProjectPilotService.deleteYfProjectPilots(yfProjectPilotForm);
		String serverPath=CommonUtil.ATTACHE_FILE;
		if(StringUtils.isBlank(serverPath)){
			serverPath=request.getSession().getServletContext().getRealPath("/"); 
		}
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(yfProjectPilotForm.getId().toString());
		attachmentForm.setType(com.manager.commons.Constants.FJ_TYPE_PILOT);
		List<Attachment> attachment = attachmentService.queryAttachment(attachmentForm);
		if(attachment!=null && attachment.size()>0){
			for (Attachment attachment2 : attachment) {
				File file = new File(serverPath + attachment2.getUrl());
		        file.delete();
				attachmentService.deleteObject(attachment2);
				
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfProjectPilot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfProjectPilots");
		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		yfProjectPilot = this.yfProjectPilotService.searchYfProjectPilot(yfProjectPilotForm, null).get(0);
		request.setAttribute(Constants.YFPROJECTPILOT, yfProjectPilot);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		ApplicationUtil au = new ApplicationUtil();
		List<Usertable> userList =  au.getUserByName("研发经理",null);
		request.setAttribute("userList", userList);
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfProjectPilot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfProjectPilots");
		YfProjectPilot yfProjectPilot = this.getYfProjectPilot(form);
	        yfProjectPilot.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfProjectPilot.setCreateDate(new Date());
		this.yfProjectPilotService.updateObject(yfProjectPilot);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfProjectPilot(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfProjectPilot");
		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		yfProjectPilot = this.yfProjectPilotService.searchYfProjectPilot(yfProjectPilotForm, null).get(0);
		request.setAttribute(Constants.YFPROJECTPILOT, yfProjectPilot);
		String processId = yfProjectPilot.getProcessId();
		if(StringUtils.isNotBlank(processId)){
			examineList(processId,yfExmaineService,request);
		}
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());

		return mapping.findForward(Constants.SUCCESS);
	}

	// get--set
	/**
	 * @return the YfProjectPilotService
	 */
	public YfProjectPilotService getYfProjectPilotService() {
		return this.yfProjectPilotService;
	}

	/**
	 * @param YfProjectPilotService
	 *  the YfProjectPilotService to set
	 */
	public void setYfProjectPilotService(YfProjectPilotService yfProjectPilotService) {
		this.yfProjectPilotService = yfProjectPilotService;
	}

	/**
	 * 获取YfProjectPilot实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfProjectPilot
	 */
	public YfProjectPilot getYfProjectPilot(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		yfProjectPilot = new YfProjectPilot();
		BeanUtils.copyProperties(yfProjectPilot, yfProjectPilotForm);
		return yfProjectPilot;
	}
    
    public void setYfProjectPilot(YfProjectPilot yfProjectPilot) {
		this.yfProjectPilot = yfProjectPilot;
	}

	public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

	public WorkFlowCommon getWorkFlowCommon() {
		return workFlowCommon;
	}

	public void setWorkFlowCommon(WorkFlowCommon workFlowCommon) {
		this.workFlowCommon = workFlowCommon;
	}

	public YfExmaineService getYfExmaineService() {
		return yfExmaineService;
	}

	public void setYfExmaineService(YfExmaineService yfExmaineService) {
		this.yfExmaineService = yfExmaineService;
	}
	
	/**
	 * 提交审核
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward submitWorkFlowYfProjectPilot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("submitWorkFlowYfProjectPilot");
		String submitType = request.getParameter("submitType");
		//新增界面直接提交
		if(StringUtils.isNotBlank(submitType) && "add".equals(submitType)){
			yfProjectPilot = this.getYfProjectPilot(form);
			yfProjectPilot.setOperateid(CommonUtil.getCurrentUser(request).getId());
			yfProjectPilot.setCreateDate(new Date());
			this.yfProjectPilotService.insertObject(yfProjectPilot);
		}
		if(StringUtils.isNotBlank(submitType) && "update".equals(submitType)){
			yfProjectPilot = this.getYfProjectPilot(form);
			YfProjectPilotForm tempForm = new YfProjectPilotForm();
			tempForm.setId(yfProjectPilot.getId());
			YfProjectPilot old = yfProjectPilotService.searchYfProjectPilot(tempForm,null).get(0);
			yfProjectPilot.setProjectType(old.getProjectType());
			yfProjectPilot.setProjectOrg(old.getProjectName());
			
		}else{
			YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
			yfProjectPilot = yfProjectPilotService.searchYfProjectPilot(yfProjectPilotForm, null).get(0);
		}
		String yfjl = "";
		ApplicationUtil au = new ApplicationUtil();
		List<Usertable> userList = au.getUserByName("研发经理", yfProjectPilot.getProjectOrg());
		if(userList!=null && userList.size()>0){
			yfjl = userList.get(0).getId();
		}
		
		Map<String,String> mapVar = new HashMap<String,String>();
		mapVar.put("bussId", yfProjectPilot.getId().toString());
		mapVar.put("bussType", WorkFlowConstants.BUSS_TYPE_ZS.toString());
		mapVar.put("projectName", yfProjectPilot.getProjectName().toString());
		mapVar.put("projectType", yfProjectPilot.getProjectType().toString());
		mapVar.put("projectOrg", yfProjectPilot.getProjectOrg().toString());
		mapVar.put("projectNo", yfProjectPilot.getProjectNo().toString());
		mapVar.put("ownYear","0");  //中试 
		mapVar.put("yfjl", yfjl); //放置研发经理

		/**
		 * TO DO  流程发起人需要根据实际用户修改
		 */
		String processId = workFlowCommon.startFlow(WorkFlowConstants.PORCESS_DEFINITION_ZS,CommonUtil.getCurrentUser(request).getId(),mapVar);
	
		yfProjectPilot.setProcessId(processId);
		yfProjectPilot.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_2);
		yfProjectPilot.setOperateid(CommonUtil.getCurrentUser(request).getId());
		yfProjectPilot.setCreateDate(new Date());
		this.yfProjectPilotService.updateObject(yfProjectPilot);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 进入审核页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAuditYfProjectPilot(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("toAuditYfProjectPilot");
		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		String bussId = request.getParameter("bussId");
		String processId = request.getParameter("processId");
		String workItemId = request.getParameter("workItemId");
		String bussType = request.getParameter("bussType");
		request.setAttribute("bussType",bussType);
		yfProjectPilotForm.setId(bussId);
		yfProjectPilot = yfProjectPilotService.searchYfProjectPilot(yfProjectPilotForm, null).get(0);
		yfProjectPilot.setWorkItemId(workItemId);
		request.setAttribute(Constants.YFPROJECTPILOT, yfProjectPilot);
		examineList(processId,yfExmaineService,request);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());

		return mapping.findForward(Constants.SUCCESS);
	}
	
	
	/**
	 * 处理审批 提交流程
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward commandFlowYfProjectPilot(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("commandFlowYfProjectPilot");
		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		yfProjectPilot = yfProjectPilotService.searchYfProjectPilot(yfProjectPilotForm, null).get(0);
		YfExmaine examine = addYfExmaine(yfProjectPilotForm,request);
		if(examine.getExamineFlag().equals("chakan")){
			String workItemId = yfProjectPilotForm.getWorkItemId();
			Map processVar = new HashMap();
			workFlowCommon.commandFlow(workItemId, processVar);
		}else{
			Map processVar = new HashMap();
			yfExmaineService.insertObject(examine);
			List actorList = new ArrayList();
			if(examine.getExamineFlag().equals(WorkFlowConstants.AUDIT_STATUS_AGREE)){
				yfProjectPilot.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_3);
				//审核通过发送给项目经理和项目组成员
				YfProjectTeamsForm yfProjectTeamsForm = new YfProjectTeamsForm();
				yfProjectTeamsForm.setProjectNo(yfProjectPilot.getProjectNo());
				List<YfProjectTeams> teams = yfProjectTeamsService.searchYfProjectTeams(yfProjectTeamsForm, null);
				if(teams!=null && teams.size()>0){
					for (YfProjectTeams yfProjectTeams : teams) {
						actorList.add(yfProjectTeams.getUserId());
					}
				}
			}else if(examine.getExamineFlag().equals(WorkFlowConstants.AUDIT_STATUS_DISAGREE)){
				yfProjectPilot.setAuditStatus(WorkFlowConstants.AUDIT_STATUS_4);
				//审核不通过发送给项目经理
				actorList.add(yfProjectPilot.getProjectManager());
			}
			yfProjectPilotService.updateObject(yfProjectPilot);
			processVar.put("bussId", yfProjectPilot.getId().toString());
			processVar.put("bussType", WorkFlowConstants.BUSS_TYPE_ZS.toString());
			processVar.put("projectName", yfProjectPilot.getProjectName().toString());
			processVar.put("projectType", yfProjectPilot.getProjectType().toString());
			processVar.put("projectOrg", yfProjectPilot.getProjectOrg().toString());
			processVar.put("projectNo", yfProjectPilot.getProjectNo().toString());
			processVar.put("ownYear", "0");
			processVar.put("yfbussTaskType",WorkFlowConstants.BUSS_TASK_TYPE_VIEW);
			String workItemId = yfProjectPilotForm.getWorkItemId();
			workFlowCommon.commandFlowDynamicAssign(workItemId, processVar,actorList);
		}
		
	
	
		
		return mapping.findForward(Constants.SUCCESS);
	}
	
	/**
	 * Description: TODO 组合审批意见对象,添加
	 * 
	 * @param form
	 * @param request
	 * @return ActionForward
	 */
	public YfExmaine addYfExmaine(ActionForm form, HttpServletRequest request) {

		YfProjectPilotForm yfProjectPilotForm = (YfProjectPilotForm) form;
		// 审核标识
		String examineFlag = yfProjectPilotForm.getExamineFlag();
		// 审核意见
		String examineOption = yfProjectPilotForm.getExamineOption();
		String processId = yfProjectPilotForm.getProcessId();
		YfExmaine yfExmaine = new YfExmaine();
		 
		yfExmaine.setId(RandomKey.generateID_Long_19());
		yfExmaine.setExamineFlag(examineFlag);
		yfExmaine.setExamineOption(examineOption);
		yfExmaine.setProcessId(processId);
		yfExmaine.setExamineDate(new Date());
		if(request.getSession().getAttribute("loginUser")!=null){
			Usertable curUser = (Usertable) request.getSession().getAttribute("loginUser");
			yfExmaine.setExaminePerson(curUser.getRname());
			yfExmaine.setExamineOrg("研发经理");
			
		}
		return yfExmaine;
	}
	
	 /*
	 * Description: TODO 查看显示意见
	 * 
	 * @param processInsId
	 * @param examine
	 * @param examineService
	 * @param examineLis
	 * @param request
	 *        void
	 */
	public void examineList(String processInsId, YfExmaineService yfExmaineService, HttpServletRequest request) throws Exception {
		YfExmaineForm yfExmaineForm = new YfExmaineForm();
		yfExmaineForm.setProcessId(processInsId);
		// 审批列表
		List<YfExmaine> examineList = yfExmaineService.searchYfExmaine(yfExmaineForm, null);
		// 防止页面出错
		int resultSize = 0;
		if(examineList != null && !examineList.isEmpty()) {
			resultSize = examineList.size();
		}
		DisplayTagPageBean.getPageBean("examine", resultSize, 0, request);
		request.setAttribute("examineList", examineList);

	}

	public YfProjectTeamsService getYfProjectTeamsService() {
		return yfProjectTeamsService;
	}

	public void setYfProjectTeamsService(YfProjectTeamsService yfProjectTeamsService) {
		this.yfProjectTeamsService = yfProjectTeamsService;
	}

}
